import nano from "./lib/js/uni.webview.1.5.4";
import {watchEffect} from 'vue'
export default {
  install() {
    nano.addEvent = (name, fn) => {
      window[name] = fn;
      nano[name] = window[name];
    };
    nano.sendMsg = function (message) {
    nano.postMessage({ data: message });
    };
    const registrationList = [
      {
        name: "addDialog",
        fn: () => {
          let _dialog_status = {};
          return (status = undefined,) => {
            if (status) {
              const key = Object.keys(_dialog_status).length+''
              _dialog_status[key] = status;
              watchEffect(()=>{
                if(_dialog_status[key].value){
                  nano.sendMsg('dialog')
                }
              })
            } else {
              if (_dialog_status) {
                for(let key in _dialog_status){
                  _dialog_status[key].value = false;
                }
              }
            }
          };
        },
      },
      {
        name: "getLocation",
        fn: () => {
          let _location_callback = undefined;
          return (
            callback = undefined,
            location_info = null,
            fail_msg = null
          ) => {
            if (callback) {
              nano.sendMsg("get_location");
              _location_callback = callback;
            } else {
              if (location_info) {
                _location_callback(true, location_info);
              } else {
                _location_callback(false, fail_msg);
              }
            }
          };
        },
      },
      {
        name: "addStyle",
        fn: () => {
          return (style) => {
            nano.sendMsg({
              name: "web_view_style",
              style,
            });
          };
        },
      },
    ];
    registrationList.forEach((item) => {
      const variable = item.fn();
      nano.addEvent(item.name, variable);
    });
  },
};
export { nano };
